package com.zm.admin.zmeng.controller;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zm.admin.annotation.SysLogTag;
import com.zm.admin.utils.ResultBean;
import com.zm.admin.zmeng.domain.ZmAttachment;
import com.zm.admin.zmeng.dto.AttachmentDto;
import com.zm.admin.zmeng.service.ZmAttachmentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/***
 *
 * @Auther: MuYun
 * @Date: 2023/2/14 9:14
 * @Description： 附件管理相关接口
 *
 */
@RequestMapping("/attachment")
@RestController
@Slf4j
public class AttachmentController {

    @Resource
    private ZmAttachmentService zmAttachmentService;

    /**
     * 获取附件列表
     * @param attachmentDto
     * @return
     */
    @RequestMapping("/list")
    public ResultBean list(AttachmentDto attachmentDto){
        ResultBean resultBean=new ResultBean();
        Integer pageNo=1;
        Integer pageSize=10;
        try{
            if(ObjectUtil.isNotEmpty(attachmentDto.getPageNo())){
                pageNo=attachmentDto.getPageNo();
            }
            if(ObjectUtil.isNotEmpty(attachmentDto.getPageSize())){
                pageSize=attachmentDto.getPageSize();
            }
            QueryWrapper<ZmAttachment> wrapper = designWrapper(attachmentDto);
            Page<ZmAttachment> page = zmAttachmentService.page(new Page<>(pageNo, pageSize), wrapper);
            resultBean.setCode(ResultBean.CODE_SUCCESS).setData(page);
        }catch (Exception e){
            log.error("获取附件列表接口报错:{}",e);
            resultBean.setCode(ResultBean.CODE_ERROR).setMsg("获取附件列表接口报错");
        }
        return resultBean;
    }

    /**
     * 上传
     * @param request
     * @param file
     * @return
     */
    @RequestMapping("/upload")
    @SysLogTag(desc = "上传附件")
    public ResultBean upload(HttpServletRequest request, MultipartFile file){
        ResultBean resultBean=new ResultBean();
        try{
            zmAttachmentService.upload(request,file,"附件上传");
            resultBean.setCode(ResultBean.CODE_SUCCESS).setMsg("上传成功");
        }catch (Exception e){
            log.error("文件上传接口报错:{}",e);
            resultBean.setCode(ResultBean.CODE_ERROR).setMsg("文件上传接口报错");
        }
        return resultBean;
    }

    /**
     * 下载
     * @param attachment
     */
    @RequestMapping("/download")
    @SysLogTag(desc = "下载附件")
    public void download(ZmAttachment attachment){
        try{
            zmAttachmentService.download(attachment);
        }catch (Exception e){
            log.error("附件下载接口报错:{}",e);
        }
    }

    /**
     * 文件删除（含批量
     * @param list
     * @return
     */
    @RequestMapping("/del")
    @SysLogTag(desc = "文件删除")
    public ResultBean del(@RequestBody List<ZmAttachment> list){
        return zmAttachmentService.del(list);
    }

    public QueryWrapper<ZmAttachment> designWrapper(AttachmentDto attachmentDto){
        log.info("attachmentDto:{}",attachmentDto);
        QueryWrapper<ZmAttachment> wrapper=new QueryWrapper<>();
        if(StrUtil.isNotEmpty(attachmentDto.getOriginalName())){
            wrapper.lambda().like(ZmAttachment::getOriginalName,attachmentDto.getOriginalName());
        }
        if(StrUtil.isNotEmpty(attachmentDto.getType())){
            wrapper.lambda().eq(ZmAttachment::getType,attachmentDto.getType());
        }
        if(StrUtil.isNotEmpty(attachmentDto.getStartTime())){
            wrapper.lambda().ge(ZmAttachment::getCreateTime,attachmentDto.getStartTime());
        }
        if(StrUtil.isNotEmpty(attachmentDto.getEndTime())){
            wrapper.lambda().le(ZmAttachment::getCreateTime,attachmentDto.getEndTime());
        }
        wrapper.select(ZmAttachment.class,i->!"absolutePath".equals(i.getColumn()));
        return wrapper;
    }
}
